package bean;

import bean.Being.Hunter;
import bean.Resource.ResourceTest;
import bean.dao.NewsDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.util.Date;
import java.util.Locale;

/**
 * 测试Sping容器和Spring Bean
 */
public class testContainer {
    public static void main(String[] args) throws InterruptedException {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

        //测试事务管理
        NewsDao dao = applicationContext.getBean("newsDao",NewsDao.class);
        dao.insert("java","java ee");

        //测试Resource作为bean属性
//        ResourceTest resourceTest = applicationContext.getBean("resourceTest",ResourceTest.class);
//        resourceTest.view();

        //测试ResourceLoader接口
//        Resource resource = applicationContext.getResource("classpath:beans.xml");
//        System.out.println(resource.getDescription());
//        System.out.println(resource instanceof ClassPathResource);

        //测试Bean后处理器
//        Person person = applicationContext.getBean("person", Person.class);

        //测试获取方法返回值
//        applicationContext.getBean("win");

        //测试获取Field值
//        System.out.println(applicationContext.getBean("connection"));

        //测试使用其他bean的属性作为set的参数
//        Person person = applicationContext.getBean("person2",Person.class);
//        System.out.println(person.getSchools());

        //测试lookup-method属性
//        Hunter hunter = applicationContext.getBean("hunter",Hunter.class);
//        Hunter hunter1 = applicationContext.getBean("hunter",Hunter.class);
//        hunter.testBeing();
//        System.out.println(hunter == hunter1);
//        System.out.println(hunter.getDog() == hunter1.getDog());

        //使用AbstractApplicationContext的registerShutDown方法关闭容器，测试destroy-method
//        AbstractApplicationContext actx = new ClassPathXmlApplicationContext("beans.xml");
//        Person person = actx.getBean("person",Person.class);
//        actx.registerShutdownHook();

        //获取bean的id，实现BaseNameAware接口
//        Person person = applicationContext.getBean("person",Person.class);
//        person.info();
        //测试工厂Bean
//        System.out.println(applicationContext.getBean("north"));

        //测试bean继承
//        Person person = applicationContext.getBean("child",Person.class);
//        System.out.println("合并了parent的属性"+person.getSchools());

        //测试注入集合值
//        Person person = applicationContext.getBean("person",Person.class);
//        System.out.println(person.getSchools());
//        System.out.println(person.getScores());

        //测试bean的不同作用域
//        Person p1 = applicationContext.getBean("person",Person.class);
//        Person p2 = applicationContext.getBean("person",Person.class);
//        Person p3 = applicationContext.getBean("person1",Person.class);
//        Person p4 = applicationContext.getBean("person1",Person.class);
//        System.out.println("singleton作用域实例: "+(p1==p2));
//        System.out.println("prototype作用域实例: "+(p3==p4));

        //bean类调用spring容器
//        person.sayHi();

        //国际化测试
//        String hello = applicationContext.getMessage("hello",new String[]{"zz"},
//                Locale.getDefault(Locale.Category.FORMAT));
//        String now = applicationContext.getMessage("now",new Object[]{new Date()},
//                Locale.getDefault(Locale.Category.FORMAT));
//        System.out.println(hello+"\n"+now);
    }
}
